package com.happyscan138.makefun;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.VideoView;

import com.squareup.picasso.Picasso;

import static com.loopj.android.http.AsyncHttpClient.log;

/**
 * Created by Administrator on 2017/3/13.
 */

public class VideoPlay extends AppCompatActivity{
    private ImageView Img;
    private ProgressBar progressBar;
    private VideoView videoView;
    private Intent intent;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.TYPE_STATUS_BAR, WindowManager.LayoutParams.TYPE_STATUS_BAR);
        setContentView(R.layout.videoplay);
        initView();
        intent=getIntent();
        String resource=intent.getStringExtra("Thumb");
        final String url=intent.getStringExtra("url");
        log.e("thumb",resource);
        Picasso.with(this).load(resource).into(Img);
        videoView.setVideoPath(url);
        videoView.start();
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        Handler handler=new Handler();
         Runnable runnable=new Runnable() {
            @Override
            public void run() {
                if(videoView.isPlaying()){
                    log.e("videoview","bbbbbbbbbbbbbbbbbbbbb");

                }else {
                    log.e("videoview","aaaaaaaaaaaaaaaaaaaaaaaa");
                    videoView.setVisibility(View.VISIBLE);
                    progressBar.setVisibility(View.VISIBLE);
                    Img.setVisibility(View.VISIBLE);
                }
            }
        };
        handler.postDelayed(runnable,500);

        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                Img.setVisibility(View.VISIBLE);
                progressBar.setVisibility(View.VISIBLE);
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                videoView.start();
            }
        });
    }
    public void initView(){
        Img=(ImageView)findViewById(R.id.video_img);
        videoView=(VideoView)findViewById(R.id.videoplay);
        progressBar=(ProgressBar)findViewById(R.id.video_pro);
        videoView.setVisibility(View.GONE);
    }

}
